package cn.northsea.mapper;

import cn.northsea.pojo.Menu;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MenuMapper {


    @Select("select distinct m.*\n" +
            "from erp_user u\n" +
            "     inner join erp_user_role ur on u.user_id = ur.user_id\n" +
            "            inner join erp_role r on r.role_id = ur.role_id\n" +
            "            inner join erp_role_menu rm on rm.role_id = r.role_id\n" +
            "            inner join erp_menu m on m.menu_id = rm.menu_id\n" +
            "            where u.user_id = #{value} and m.menu_pid = 0")
    List<Menu> findMenuOneByUid(Integer id);

    @Select("select * from erp_menu")
    List<Menu> findAll();

    @Select("select m.*\n" +
            "from erp_role_menu rm inner join erp_menu m\n" +
            "on rm.menu_id = m.menu_id\n" +
            "where rm.role_id = #{value}")
    List<Menu> findMenuByRid(Integer role_id);

    @Delete("delete from erp_role_menu where role_id = #{value}")
    void delMenu(Integer role_id);


    void allocationMenu(@Param("role_id") Integer role_id, @Param("mids") List<Integer> mids);


    @Select("select distinct m.*\n" +
            "from erp_user u\n" +
            "     inner join erp_user_role ur on u.user_id = ur.user_id\n" +
            "            inner join erp_role r on r.role_id = ur.role_id\n" +
            "            inner join erp_role_menu rm on rm.role_id = r.role_id\n" +
            "            inner join erp_menu m on m.menu_id = rm.menu_id\n" +
            "            where u.user_id = #{value} and m.menu_pid != 0")
    List<Menu> findMenuByUid(Integer userId);
}
